﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

using Tools.Json;

using SY.WebSite.BLL;
using SY.WebSite.Entity;

namespace SY.WebSite.Web.Handler
{
    public class AuctionManageHandler : AdminWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "getList":
                    GetList();
                    break;
                case "showResult":
                    ShowResult();
                    break;
                case "charge":
                    Charge();
                    break;
                case "add":
                    Add();
                    break;
                case "edit":
                    Edit();
                    break;
                case "updateItem":
                    UpdateItem();
                    break;
                case "getItemList":
                    GetItemList();
                    break;
                case "deleteItem":
                    DeleteItem();
                    break;
            }

            context.Response.End();
        }

        public void DeleteItem() {
            int auctionId = Tools.Web.Request.GetFormInt("auctionId", 0);
            int itemId = Tools.Web.Request.GetFormInt("itemId", 0);

            if (itemId <= 0 || auctionId <= 0)
                BreakResponse();

            List<Auction> list = BizAuction.All().ToList();
            Auction auction = list.FirstOrDefault(l => l.Id == auctionId && l.Status == 0);
            if (auction == null)
                BreakResponse();

            if (auction.StartTime <= DateTime.Now)
                BreakResponse("拍卖开始之后，不能删除物品");

            AuctionItem item = BizAuctionItem.GetByKey(itemId);
            if (item.Status !=0)
                BreakResponse();

            item.Status = -1;

            int result = BizAuctionItem.Update(item);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void GetItemList() {
            int auctionId = RequestIntParam("auctionId");
            if (auctionId <= 0)
                BreakResponse();

            Response.Write(new JsonTransfer().Add("list", BizAuctionItem.GetListByAuctionId(auctionId)));            
        }

        public void Edit()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);
            string title = Tools.Web.Request.GetFormString("title");
            string logo = Tools.Web.Request.GetFormString("logo");
            string description = Tools.Web.Request.GetFormString("description");
            string start = Tools.Web.Request.GetFormString("start");
            string end = Tools.Web.Request.GetFormString("end");

            if (id <= 0 || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(logo) || string.IsNullOrEmpty(description)
                || string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
                BreakResponse();

            DateTime startTime;
            DateTime endTime = DateTime.Now;
            if (!DateTime.TryParse(start, out startTime) || !DateTime.TryParse(end, out endTime))
                BreakResponse();

            if (startTime >= endTime)
                BreakResponse("开始时间不能大于结束时间");

            List<Auction> list = BizAuction.All().ToList();

            Auction auction = list.FirstOrDefault(l => l.Id == id && l.Status==0);
            if (auction == null)
                BreakResponse();

            if (auction.StartTime <= DateTime.Now)
                BreakResponse("拍卖开始之后，不能编辑基本信息");

            if (list.Exists(l => l.Id != id && l.Status == 0 && ((l.StartTime >= startTime && l.StartTime <= endTime) || (l.EndTime >= startTime && l.EndTime <= endTime))))
                BreakResponse("当前时间已经存在一场竞拍，请选择其他时间段");

            auction.Description = Tools.StringUtil.HtmlEncode(description);
            auction.Title = Tools.StringUtil.HtmlEncode(title);
            auction.StartTime = startTime;
            auction.EndTime = endTime;
            auction.Image = Tools.StringUtil.HtmlEncode(logo);

            int result = BizAuction.Update(auction);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void UpdateItem()
        {
            int auctionId = Tools.Web.Request.GetFormInt("auctionId", 0);
            int itemId = Tools.Web.Request.GetFormInt("itemId", 0);
            string title = Tools.Web.Request.GetFormString("title");
            string logo = Tools.Web.Request.GetFormString("logo");
            string description = Tools.Web.Request.GetFormString("description");
            int price = Tools.Web.Request.GetFormInt("price", 0);

            if (auctionId<=0 || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(logo) || string.IsNullOrEmpty(description))
                BreakResponse();

            List<Auction> list = BizAuction.All().ToList();
            Auction auction = list.FirstOrDefault(l => l.Id == auctionId && l.Status == 0);
            if (auction == null)
                BreakResponse();

            if (auction.StartTime <= DateTime.Now)
                BreakResponse("拍卖开始之后，不能更新物品");

            AuctionItem item = new AuctionItem
            {
                Id = itemId,
                AuctionId = auctionId,
                Title = Tools.StringUtil.HtmlEncode(title),
                Description = Tools.StringUtil.HtmlEncode(description),
                Price = price,
                Image = Tools.StringUtil.HtmlEncode(logo),
                Status = 0
            };

            int result = itemId > 0 ? BizAuctionItem.Update(item) : BizAuctionItem.Add(item);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void Add() {
            string title = Tools.Web.Request.GetFormString("title");
            string logo = Tools.Web.Request.GetFormString("logo");
            string description = Tools.Web.Request.GetFormString("description");
            string start = Tools.Web.Request.GetFormString("start");
            string end = Tools.Web.Request.GetFormString("end");

            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(logo) || string.IsNullOrEmpty(description)
                || string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
                BreakResponse();

            DateTime startTime;
            DateTime endTime = DateTime.Now;
            if (!DateTime.TryParse(start, out startTime) || !DateTime.TryParse(end, out endTime))
                BreakResponse();

            if (startTime >= endTime)
                BreakResponse("开始时间不能大于结束时间");

            List<Auction> list = BizAuction.All().ToList();
            if (list.Exists(l => l.Status == 0 && ((l.StartTime >= startTime && l.StartTime <= endTime) || (l.EndTime >= startTime && l.EndTime <= endTime))))
                BreakResponse("当前时间已经存在一场竞拍，请选择其他时间段");

            Auction auction = new Auction
            {
                Title = Tools.StringUtil.HtmlEncode(title),
                Description = Tools.StringUtil.HtmlEncode(description),
                Image = Tools.StringUtil.HtmlEncode(logo),
                CreateBy = CurrentUser.UserId,
                StartTime = startTime,
                EndTime = endTime,
                Count = 0,
                Status = 0,
                Type = 1,
                CreateTime = DateTime.Now
            };

            int result = BizAuction.Add(auction);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }

            BreakResponse();
        }

        public void Charge() {
            int auctionId = Tools.Web.Request.GetFormInt("auctionId", 0);
            if (auctionId <= 0)
                BreakResponse();

            Auction auction = BizAuction.GetByKey(auctionId);
            if (auction == null || auction.Status != 0 || auction.EndTime > DateTime.Now)
                BreakResponse();

            if (BizAuction.Charge(auctionId))
            {
                Response.Write(new JsonTransfer().Add("success", ""));
                Response.End();
            }
            BreakResponse();
        }

        public void ShowResult()
        {
            int auctionId = RequestIntParam("auctionId");
            if (auctionId <= 0)
                BreakResponse();

            List<Hashtable> list = BizAuctionItem.GetAuctionResult(auctionId);
            Response.Write(new JsonTransfer().Add("list", list));
        }

        public void GetList()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string title = RequestStringParam("title");
            int spstatus = RequestIntParam("spstatus", 100);
            string start = RequestStringParam("start");
            string end = RequestStringParam("end");

            if (spstatus < 0)
                BreakResponse();

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Auction> list = BizAuction.All();
            switch (spstatus) { 
                case 0:
                    list = list.Where(l => l.StartTime > DateTime.Now);
                    break;
                case 1:
                    list = list.Where(l => l.StartTime <= DateTime.Now && l.EndTime >= DateTime.Now);
                    break;
                case 2:
                    list = list.Where(l => l.EndTime <= DateTime.Now && l.Status != 1);
                    break;
                case 3:
                    list = list.Where(l => l.Status == 1);
                    break;
            }

            if (!string.IsNullOrEmpty(title))
                list = list.Where(l => l.Title.Contains(title));

            DateTime temp;
            if (!string.IsNullOrEmpty(start) && DateTime.TryParse(start, out temp))
                list = list.Where(l => l.StartTime >= temp);

            DateTime endTemp;
            if (!string.IsNullOrEmpty(end) && DateTime.TryParse(end, out endTemp))
                list = list.Where(l => l.EndTime <= endTemp);

            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));
            jt.Add("count", list.Count());
            Response.Write(jt);
        }
    }
}